package com.bootdo.common.utils.httpUtils;

import net.taldius.clamav.ClamAVScanner;
import net.taldius.clamav.ClamAVScannerFactory;

import java.io.FileInputStream;
import java.io.InputStream;

public class ClamAVFileScan {

    private ClamAVScanner scanner;

    public String clamdHost = "127.0.0.1";

    public int clamdPort = 3310;

    public int connectionTimeout = 1000;

    //  private static Logger logger = Logger.getLogger(ClamAVFileScan.class);
    public static void main(String args[]) {
        boolean scanResult = false;
        ClamAVFileScan clamAVFileScan = new ClamAVFileScan();

        try {
            clamAVFileScan.initScanner();

            long startTime = System.currentTimeMillis();
            scanResult = clamAVFileScan.fileScanner("C:\\Users\\Administrator\\Desktop\\病毒测试用例.jsp");

            long endTime = System.currentTimeMillis();

            System.out.println(endTime - startTime + "ms passed.");
            if (scanResult) {
                System.out.println("No virus found");
            } else {
                System.out.println("Warning!!!! Virus found");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initScanner() {

//        ClamAVScannerFactory.setClamdHost("127.0.0.1"); // Host ip where 'clamd' process is running
        ClamAVScannerFactory.setClamdHost(clamdHost); // Host ip where 'clamd' process is running
        ClamAVScannerFactory.setClamdPort(clamdPort); // Port on which 'clamd' process is listening
        ClamAVScannerFactory.setConnectionTimeout(connectionTimeout);// Connection time out to connect 'clamd' process

        this.scanner = ClamAVScannerFactory.getScanner();
    }

    public boolean fileScanner(String fileInputStream) throws Exception {

        boolean resScan = false;
        if (fileInputStream != null) {
            InputStream file = new FileInputStream(fileInputStream);
            System.out.println("读取到的流的大小是：" + file.available());
            resScan = scanner.performScan(file);

        } else {

            throw new Exception();
        }
        return resScan;
    }

    /**
     * 执行扫描文件的的方法
     * 输入参数为文件的绝对路径
     * 返回值-1为有病毒，1为无病毒
     *
     * @param fileInputStream
     * @return
     */
    public static int ScanFile(String fileInputStream) {
        boolean scanResult = false;
        ClamAVFileScan clamAVFileScan = new ClamAVFileScan();
        try {
            clamAVFileScan.initScanner();

            long startTime = System.currentTimeMillis();
            scanResult = clamAVFileScan.fileScanner(fileInputStream);

            long endTime = System.currentTimeMillis();

            System.out.println(endTime - startTime + "ms passed.");
            if (scanResult) {
                System.out.println("No virus found");
                return 1;
            } else {
                System.out.println("Warning!!!! Virus found");
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 执行扫描文件的的方法
     * 输入参数为文件的绝对路径
     * 返回值-1为有病毒，1为无病毒
     *
     * @param fileInputStream
     * @return
     */
    public static int ScanFileP(String fileInputStream) {
        boolean scanResult = false;
        ClamAVFileScan clamAVFileScan = new ClamAVFileScan();
        try {
            clamAVFileScan.initScanner();

            long startTime = System.currentTimeMillis();
            scanResult = clamAVFileScan.fileScanner(fileInputStream);

            long endTime = System.currentTimeMillis();

            System.out.println(endTime - startTime + "ms passed.");
            if (scanResult) {
                System.out.println("No virus found");
                return 1;
            } else {
                System.out.println("Warning!!!! Virus found");
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

}
